<?php
namespace App\Domain;
use App\Model\Dynamic as Model_Dynamic;


class Dynamic {

    /* 发布动态 */
	public function setDynamic($data) {
		$rs = array('code' => 0, 'msg' => \PhalApi\T('发布成功，请等待审核'), 'info' => array());
        
        $uid=$data['uid'];
        $thumb=$data['thumb'];
        $video_thumb=$data['video_thumb'];
        $href=$data['href'];
        
//        $isauth=\App\isAuthorAuth($uid);
//        if(!$isauth){
//            $rs['code']=1003;
//			$rs['msg']=\PhalApi\T('您还未进行主播认证或主播认证还未通过');
//			return $rs;
//        }
        
        $nowtime=time();
		$status='0';
		//审核开关
		$configpri=\App\getConfigPri();
		$dynamic_switch=$configpri['dynamic_switch'];
		if($dynamic_switch=='0'){   //动态发布审核关闭：动态状态默认：通过
			$status='1';//
			$rs['msg']=\PhalApi\T("发布成功");
		}
        $data['status']=$status;
        $data['addtime']=$nowtime;
        $data['uptime']=$nowtime;
        
		$model = new Model_Dynamic();
        $result = $model->setDynamic($data);

        if(!$result){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('发布失败，请重试');
			return $rs;
        }
		
		//每日任务
		$dailytask_data=[
			'type'=>'publish_dynamic',
			'nums'=>1
		];

		\App\dailyTasks($uid,$dailytask_data);
     
		return $rs;
	}
	/* 评论动态 */
	public function setComment($data) {
        $rs = array();
        $model = new Model_Dynamic();
        $rs = $model->setComment($data);

        return $rs;
    }
	
	 /* 我的视频 */
	public function myVideo($uid,$p) {

        $model = new Model_Dynamic();
        $list= $model->myVideo($uid,$p);
        
        $userinfo=\App\getUserInfo($uid);
        
        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['thumb_s']=\App\get_upload_path($v['thumb_s']);
            $v['href']=\App\get_upload_path($v['href']);
            $v['likes']=\App\NumberFormat($v['likes']);
            $v['views']=\App\NumberFormat($v['views']);
            $v['shares']=\App\NumberFormat($v['shares']);
            if($v['status']<2){
                $v['reason']='';
            }
            
            $user['id']=$userinfo['id'];
            $user['user_nickname']=$userinfo['user_nickname'];
            $user['avatar']=$userinfo['avatar'];
            $user['avatar_thumb']=$userinfo['avatar_thumb'];
            
            $v['userinfo']=$user;

            
            $list[$k]=$v;
        }
        
		return $list;
	}
	
	

   

    /* 点赞 */
	public function addLike($uid,$dynamicid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());

		$model = new Model_Dynamic();
        
        $info = $model->getDynamic($dynamicid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('动态不存在');
			return $rs;
        }
        $islike = $model->islike($uid,$dynamicid);
        
        if($islike){
            /* 已点赞 - 取消 */
            $model->reduceLike($uid,$dynamicid);
            $nums=$info['likes']-1;
        }else{
            /* 未点赞 - 添加 */
            $model->addLike($uid,$dynamicid);
            $nums=$info['likes']+1;

            //每日任务
			$dailytask_data=[
				'type'=>'like_dynamic',
				'nums'=>1
			];

			\App\dailyTasks($uid,$dailytask_data);
        }

        
        if($islike == 1){
           $rs = array('code' => 0, 'msg' => \PhalApi\T('取消点赞'), 'info' => array()); 
        }else{
           $rs = array('code' => 0, 'msg' => \PhalApi\T('点赞成功'), 'info' => array()); 
        }
        $rs['info'][0]['islike']=$islike?'0':'1';
        
        $rs['info'][0]['nums']=\App\NumberFormat($nums);

		return $rs;
	}
	/* 评论/回复 点赞 */
	public function addCommentLike($uid,$commentid){
		$rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
		 
		$model = new Model_Dynamic();
        
        $info = $model->getDynamicComment($commentid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('动态评论信息不存在');
			return $rs;
        }
		
		$islike = $model->isCommentlike($uid,$commentid);
        
        if($islike){
            /* 已点赞 - 取消 */
            $model->reduceCommentLike($uid,$commentid);
            $nums=$info['likes']-1;
        }else{
            /* 未点赞 - 添加 */
            $model->addCommentLike($uid,$commentid,$info['uid'],$info['dynamicid']);
            $nums=$info['likes']+1;

            //每日任务
            $dailytask_data=[
				'type'=>'like_dynamic',
				'nums'=>1
			];

			\App\dailyTasks($uid,$dailytask_data);
        }
		  
        $rs['info'][0]['islike']=$islike?'0':'1';
        
        $rs['info'][0]['nums']=\App\NumberFormat($nums);
		
		
		return $rs; 		
	}
    

    /* 删除动态 */
	public function delDynamic($uid,$dynamicid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('删除成功'), 'info' => array());
        
		$model = new Model_Dynamic();
        
        $where="uid={$uid}";
        
        $info = $model->getDynamic($dynamicid,$where);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('动态不存在');
			return $rs;
        }
        
        if($info['uid']!=$uid){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('删除失败');
			return $rs;
        }
        
        $result = $model->delDynamic($dynamicid);

		return $rs;
	}
    
    /* 举报分类 */
	public function getReport() {
        $key='dynamic_report';
		$list=\App\getcaches($key);
		if(!$list){
            $model = new Model_Dynamic();
			$list= $model->getReport();
            if($list){
                \App\setcaches($key,$list);
            }
		}

		$lang=\PhalApi\DI()->lang;
		foreach($list as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$list[$k]=$v;
		}
		return $list;
	}

    /* 举报 */
	public function setReport($uid,$dynamicid,$content) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('举报成功，官方会马上核实'), 'info' => array());
        
		$model = new Model_Dynamic();
        
        $info = $model->getDynamic($dynamicid);
	
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('动态不存在');
			return $rs;
        }
        $nowtime=time();
        
        $data=[
            'uid'=>$uid,
            'touid'=>$info['uid'],
            'dynamicid'=>$dynamicid,
            'content'=>$content,
            'addtime'=>$nowtime,
            'uptime'=>$nowtime,
            'dynamic_type'=>$info['type'],
        ];
        $result = $model->setReport($data);
		return $rs;
	}

    /* 个人动态 */
	public function getHomeDynamic($uid,$liveuid,$p) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());

        
		$model = new Model_Dynamic();
        $where=[];
        $where['uid']=$liveuid;
        
        $userinfo=\App\getUserInfo($liveuid);
        
        $list = $model->getDynamiclists($where,$p);

        if(!$list){
        	$list=[];
        }

        if(!empty($list)){
        	foreach($list as $k=>$v){
				$v['datetime']=\App\datetime($v['addtime']);
				if(!$v['city']){
					$v['city']=\PhalApi\T("好像在火星");
				}
				if($v['thumb']){
					$thumbs=explode(";",$v['thumb']);
					foreach($thumbs as $kk=>$vv){
						if($vv){
							$thumbs[$kk]=\App\get_upload_path($vv);
						}
					}
					$v['thumbs']=$thumbs;
				}else{
					$v['thumbs']=array();
				}
				
				if($v['video_thumb']){
					$v['video_thumb']=\App\get_upload_path($v['video_thumb']);
				}
	           
			    if($v['voice']){
					$v['voice']=\App\get_upload_path($v['voice']);
				}
				if($v['href']){
					$v['href']=\App\get_upload_path($v['href']);
				}
	            
	            $v['likes']=\App\NumberFormat($v['likes']);
	            $v['comments']=\App\NumberFormat($v['comments']);
	         
	            if($v['uid']==$uid){
	                $v['islike']='0';
	            }else{
	                $v['islike']=$model->islike($uid,$v['id']);
	            }
	            
	            $user['id']=$userinfo['id'];
	            $user['user_nickname']=$userinfo['user_nickname'];
	            $user['avatar']=$userinfo['avatar'];
	            $user['avatar_thumb']=$userinfo['avatar_thumb'];
	            $user['sex']=$userinfo['sex'];
	            //ggg
	            $user['isAttention'] = \App\isAttention($uid,$v['uid']);
	            $v['userinfo']=$user;
	            
	            $list[$k]=$v;
	        }
        }
        
        

        $rs['info']=$list;

		return $rs;

	}
	
	/* 关注用户的动态 */
	public function getAttentionDynamic($uid,$p){

		$dynamic=array();
	
		$model = new Model_Dynamic();
		$attention=$model->getAttentions($uid);
	
		if($attention){
			$uids=array_column($attention,'touid');
			$touids=implode(",",$uids);
			
			$where="uid in ({$touids})  and isdel=0 and status=1";
			$dynamic = $model->getDynamiclists($where,$p);
			
			if(!$dynamic){
				return 0;
			}
			
			foreach($dynamic as $k=>$v){
				$v['datetime']=\App\datetime($v['addtime']);
				if(!$v['city']){
					$v['city']=\PhalApi\T("好像在火星");
				}
				if($v['thumb']){
					$thumbs=explode(";",$v['thumb']);
					foreach($thumbs as $kk=>$vv){
						if($vv){
							$thumbs[$kk]=\App\get_upload_path($vv);
						}
						
					}
					$v['thumbs']=$thumbs;
				}else{
					$v['thumbs']=array();
				}
				
				if($v['video_thumb']){
					$v['video_thumb']=\App\get_upload_path($v['video_thumb']);
				}
			   
				if($v['voice']){
					$v['voice']=\App\get_upload_path($v['voice']);
				}
				if($v['href']){
					$v['href']=\App\get_upload_path($v['href']);
				}
				
				$v['likes']=\App\NumberFormat($v['likes']);
				$v['comments']=\App\NumberFormat($v['comments']);
			 
				if($v['uid']==$uid){
					$v['islike']='0';
				}else{
					$v['islike']=$model->islike($uid,$v['id']);
				}
				$userinfo=\App\getUserInfo($v['uid']);
				$user['id']=$userinfo['id'];
				$user['user_nickname']=$userinfo['user_nickname'];
				$user['avatar']=$userinfo['avatar'];
				$user['avatar_thumb']=$userinfo['avatar_thumb'];
				$user['sex']=$userinfo['sex'];
				$user['country'] = $userinfo['country'];
				$user['language'] = $userinfo['language'];
				$user['level'] = $userinfo['level'];
				$user['isAttention'] = \App\isAttention($uid,$v['uid']);
                $user["levelData"] = $userinfo['levelData'];
                $user["levelAnchorData"] = $userinfo['levelAnchorData'];
                $user['online'] = $userinfo['online'];
                $user['video_value'] = $userinfo['video_value'];
                $user['voice_value'] = $userinfo['voice_value'];

				$v['userinfo']=$user;
				$dynamic[$k]=$v;
			}
		}
		if(!$dynamic){
			return 0;
		}
		return $dynamic;		
	}
	
	public function getLike($uid,$p){

		$dynamic=array();
	    $model = new Model_Dynamic();
		$dynamicids = \PhalApi\DI()->notorm->dynamics_like
                    ->where('uid=?',$uid)
                    ->select('dynamicid')
                    ->order('id desc')
                    ->fetchAll();
	
		if($dynamicids){
			$liks=array_column($dynamicids,'dynamicid');
			$liks=implode(",",$liks);
			
			$where="id in ({$liks})  and isdel=0 and status=1";
			$dynamic = $model->getDynamiclists($where,$p);
			if(!$dynamic){
				return 0;
			}
			foreach($dynamic as $k=>$v){
				$v['datetime']=\App\datetime($v['addtime']);
				if(!$v['city']){
					$v['city']=\PhalApi\T("好像在火星");
				}
				if($v['thumb']){
					$thumbs=explode(";",$v['thumb']);
					foreach($thumbs as $kk=>$vv){
						if($vv){
							$thumbs[$kk]=\App\get_upload_path($vv);
						}
						
					}
					$v['thumbs']=$thumbs;
				}else{
					$v['thumbs']=array();
				}
				
				if($v['video_thumb']){
					$v['video_thumb']=\App\get_upload_path($v['video_thumb']);
				}
			   
				if($v['voice']){
					$v['voice']=\App\get_upload_path($v['voice']);
				}
				if($v['href']){
					$v['href']=\App\get_upload_path($v['href']);
				}
				
				$v['likes']=\App\NumberFormat($v['likes']);
				$v['comments']=\App\NumberFormat($v['comments']);
			 
				if($v['uid']==$uid){
					$v['islike']='0';
				}else{
					$v['islike']=$model->islike($uid,$v['id']);
				}
				$userinfo=\App\getUserInfo($v['uid']);
				$user['id']=$userinfo['id'];
				$user['user_nickname']=$userinfo['user_nickname'];
				$user['avatar']=$userinfo['avatar'];
				$user['avatar_thumb']=$userinfo['avatar_thumb'];
				$user['sex']=$userinfo['sex'];
				$user['country'] = $userinfo['country'];
				$user['language'] = $userinfo['language'];
				$user['level'] = $userinfo['level'];
				$user['isAttention'] = \App\isAttention($uid,$v['uid']);
                $user['levelData'] = $userinfo['levelData'];
                $user['levelAnchorData'] = $userinfo['levelAuthorData'];

                $v['userinfo']=$user;
				$dynamic[$k]=$v;
			}
		}
		if(!$dynamic){
			return 0;
		}
		return $dynamic;		
	} 
	
	/*动态列表 */
	public function getDynamicList($uid,$p,$type=0){
			$dynamic=array();
	
			$model = new Model_Dynamic();
		
			$where=" isdel=0 and status=1";
			if($type){
			    $where .= " and type=".$type;
			}
			$dynamic = $model->getDynamiclists($where,$p);
			
			if(!$dynamic){
				return 0;
			}
			foreach($dynamic as $k=>$v){
				$v['datetime']=\App\datetime($v['addtime']);
				if(!$v['city']){
					$v['city']=\PhalApi\T("好像在火星");
				}
				if($v['thumb']){
					$thumbs=explode(";",$v['thumb']);
					foreach($thumbs as $kk=>$vv){
						if($vv){
							$thumbs[$kk]=\App\get_upload_path($vv);
						}
						
					}
					$v['thumbs']=$thumbs;
				}else{
					$v['thumbs']=array();
				}
				
				if($v['video_thumb']){
					$v['video_thumb']=\App\get_upload_path($v['video_thumb']);
				}
			   
				if($v['voice']){
					$v['voice']=\App\get_upload_path($v['voice']);
				}
				if($v['href']){
					$v['href']=\App\get_upload_path($v['href']);
				}
				
				$v['likes']=\App\NumberFormat($v['likes']);
				$v['comments']=\App\NumberFormat($v['comments']);
			 
				if($v['uid']==$uid){
					$v['islike']='0';
					
				}else{
					$v['islike']=$model->islike($uid,$v['id']);
				}
			
				$userinfo=\App\getUserInfo($v['uid']);
				//echo"<pre>";print_r($userinfo);exit;
				$user['id']=$userinfo['id'];
				$user['user_nickname']=$userinfo['user_nickname'];
				$user['avatar']=$userinfo['avatar'];
				$user['avatar_thumb']=$userinfo['avatar_thumb'];
				$user['sex']=$userinfo['sex'];
				$user['country'] = $userinfo['country'];//guo
				$user['language'] = $userinfo['language'];
				$user['level'] = $userinfo['level'];
				$user['video_value'] = $userinfo['video_value'];
				$user['isAttention'] = \App\isAttention($uid,$v['uid']);
				$user['online'] = $userinfo['online'];
                $user['levelData'] = $userinfo['levelData'];
                $user['levelAnchorData'] = $userinfo['levelAnchorData'];
				$v['userinfo']=$user;
				$dynamic[$k]=$v;
			}
			return $dynamic;		
	} 
	
	/* 评论列表 */
	public function getComments($uid,$dynamicid,$p){
        if($p<1){
            $p=1;
        }
		$comments=array();
	
		$model = new Model_Dynamic();
	
		$where="dynamicid='{$dynamicid}' and parentid='0'";
		$comments = $model->getComments($where,$p);
		
		if(!$comments){
			return 0;
		}
		
		foreach($comments as $k=>$v){
			$v['userinfo']=\App\getUserInfo($v['uid']);				
			$v['datetime']=\App\datetime($v['addtime']);	
			$v['likes']=\App\NumberFormat($v['likes']);	
		
			if($uid){
				$v['islike']=(string)$model->isCommentlike($uid,$v['id']);	
			}else{
				$v['islike']='0';	
			}
			
			if($v['touid']>0){
				$touserinfo=\App\getUserInfo($v['touid']);
			}
			if(!$touserinfo){
				$touserinfo=(object)array();
				$v['touid']='0';
			}
			$v['touserinfo']=$touserinfo;

			$count=$model->getCommentcount($v['id']);
			if($v['voice']){
				$v['voice']=\App\get_upload_path($v['voice']);
			}
			
			$v['replys']=$count;
            // 回复 
            $reply=$model->getReplayinfo($v['id']);
			//print_r($reply);
            foreach($reply as $k1=>$v1){
                
                $v1['userinfo']=\App\getUserInfo($v1['uid']);				
                $v1['datetime']=\App\datetime($v1['addtime']);	
                $v1['likes']=\App\NumberFormat($v1['likes']);	
                $v1['islike']=(string)$model->isCommentlike($uid,$v1['id']);
                if($v1['touid']>0){
                    $touserinfo=\App\getUserInfo($v1['touid']);
                }
                if(!$touserinfo){
                    $touserinfo=(object)array();
                    $v1['touid']='0';
                }
                
                if($v1['parentid']>0 && $v1['parentid']!=$v['id']){
                    $tocommentinfo=$model->getCommentinfo($v1['parentid']);
                }else{
                    $tocommentinfo=(object)array();
                    $touserinfo=(object)array();
                    $v1['touid']='0';
                }
                $v1['touserinfo']=$touserinfo;
                $v1['tocommentinfo']=$tocommentinfo;
				if($v1['voice']){
					$v1['voice']=\App\get_upload_path($v1['voice']);
				}
                $reply[$k1]=$v1;
            }
            
            $v['replylist']=$reply;
			$comments[$k]=$v;
		}
		
		$commentnum=$model->getDynamicount($dynamicid);
		
		$rs=array(
			"comments"=>$commentnum,
			"commentlist"=>$comments,
		);
		
		return $rs;
	}

	
	/* 回复列表 */
	public function getReplys($uid,$commentid,$last_replyid,$p){
		
		$comments=array();
	
		$model = new Model_Dynamic();
		if($last_replyid==0){ //获取回复列表里最新的一条

			$comment=$model->getReplayCommentinfo($commentid);
			$comments[]=$comment;

		}else{
			if($p<1){
				$p=1;
			}
			$where="commentid='{$commentid}' and id< '{$last_replyid}'";
			$comments = $model->getReplayComments($where,$p);
		}

		foreach($comments as $k=>$v){
			$comments[$k]['userinfo']=\App\getUserInfo($v['uid']);				
			$comments[$k]['datetime']=\App\datetime($v['addtime']);	
			$comments[$k]['likes']=\App\NumberFormat($v['likes']);	
            $comments[$k]['voice']=\App\get_upload_path($v['voice']);
			$comments[$k]['islike']=(string)$model->isCommentlike($uid,$v['id']);
			if($v['touid']>0){
				$touserinfo=\App\getUserInfo($v['touid']);
			}
			if(!$touserinfo){
				$touserinfo=(object)array();
				$comments[$k]['touid']='0';
			}

			if($v['parentid']>0 && $v['parentid']!=$commentid){
				$tocommentinfo=$model->getCommentinfo($v['parentid']);
				
			}else{

				$tocommentinfo=(object)array();
				$touserinfo=(object)array();
				$comments[$k]['touid']='0';

			}

			$comments[$k]['touserinfo']=$touserinfo;
			$comments[$k]['tocommentinfo']=$tocommentinfo;


		}

		//该评论下的总回复数
		$count=$model->getCommentcount($commentid);
		
		$res['lists']=$comments;
		$res['replys']=$count;
		
		return $res;


	}
	
	/* 获取动态信息 */
	public function getDynamic($dynamicid) {

        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
		$model = new Model_Dynamic();
        
        $info = $model->getDynamic($dynamicid);
	
		return $info;
	}
	
}
